package formbeans;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;


import org.mybeans.form.FormBean;

public class SendSurveyForm extends FormBean{
	
	
	private String userid;
	private String email;
	
	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	private Pattern pattern;
	private Matcher matcher;
	
	public String getUserid() {
		return userid;
	}
	
	public String getEmail() {
		return email;
	}
	
	public void setUserid(String userid) {
		this.userid = trimAndConvert(userid,"<>\"");
	}
	
	public void setEmail(String email) {
		this.email = trimAndConvert(email,"<>\"");
	}
	
	public List<String> checkSendSurveyFormErrors(){
		List<String> errors  = new ArrayList<String>();
		
		if(userid == null || userid.length() == 0){
			errors.add("Respondent's name is required");
		}else if(userid.length()>=255){
			errors.add("Respondent's name is too long.");
		}
		if(email == null || email.length() == 0){
			errors.add("Email is required.");
		}else if(!emailValidator(email)){
			errors.add("Invalid email");
		}
		return errors;
	}
		private boolean emailValidator(String EmailAddress){
			  pattern = Pattern.compile(EMAIL_PATTERN);
			  matcher = pattern.matcher(EmailAddress);
			  return matcher.matches();
		 }		
}
